﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Net;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.Security.Permissions;
using BaoJianSoft.Common.Properties;

namespace BaoJianSoft.Common.Web
{
    /// <summary>
    /// Base class for Restful Exceptions.
    /// </summary>
    [Serializable]
    public class RestfulException : Exception
    {
        private string _statusDescription = Resources.RestfulErrorMessage;
        private HttpStatusCode _statusCode = HttpStatusCode.InternalServerError;

        /// <summary>
        /// Http response status code.
        /// </summary>
        public HttpStatusCode StatusCode
        {
            get
            {
                return _statusCode;
            }
            set
            {
                _statusCode = value;
            }
        }

        /// <summary>
        /// Http response status description.
        /// </summary>
        public string StatusDescription
        {
            get
            {
                return _statusDescription;
            }
            set
            {
                _statusDescription = value;
            }
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public RestfulException()
        { }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="msg">Message to include in the exception.</param>
        public RestfulException(string msg) :
            base(msg)
        {
            _statusDescription = msg;
        }


        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="msg">Message to include in the exception.</param>
        /// <param name="ex">Inner exception.</param>
        public RestfulException(string msg, Exception ex)
            : base(msg, ex)
        {
            _statusDescription = msg;
        }

        /// <summary>
        /// Constructor used for serialization.
        /// </summary>
        /// <param name="info">SerializationInfo object.</param>
        /// <param name="context">StreamingContext object.</param>
        protected RestfulException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ex">Inner exception.</param>
        public RestfulException(Exception ex)
            : base(ex.Message, ex)
        {
            _statusDescription = ex.Message;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="statusCode">Http response status code.</param>
        /// <param name="ex">Inner exception.</param>
        public RestfulException(HttpStatusCode statusCode, Exception ex)
            : base(ex.Message, ex)
        {
            _statusDescription = ex.Message;
            _statusCode = statusCode;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="statusCode">Http response status code.</param>
        /// <param name="statusDescription">Message to include in the exception.</param>
        public RestfulException(HttpStatusCode statusCode, string statusDescription)
            : base(statusDescription)
        {
            _statusCode = statusCode;
            _statusDescription = statusDescription;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ex">Inner exception.</param>
        /// <param name="statusCode">Http response status code.</param>
        /// <param name="statusDescription">Http response status description.</param>
        public RestfulException(HttpStatusCode statusCode, string statusDescription, Exception ex)
            : base(ex.Message, ex)
        {
            _statusCode = statusCode;
            _statusDescription = statusDescription;
        }

        /// <summary>
        /// sets the System.Runtime.Serialization.SerializationInfo with information about the exception.
        /// </summary>
        /// <param name="info">Serialization info.</param>
        /// <param name="context">Streaming context.</param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }
    }
}

